import 'package:flutter/material.dart';
import 'package:kuwo_music/config.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class GifRefresher extends StatelessWidget {
  const GifRefresher({
    Key key,
    @required RefreshController controller,
    @required Widget child,
    this.onRefresh,
    this.onLoading,
    this.color,
  })  : _controller = controller,
        _child = child,
        super(key: key);

  final RefreshController _controller;
  final Widget _child;
  final Function onRefresh;
  final Function onLoading;
  final Color color;

  // void _onLoading() async {
  //   print('begin Load');
  //   // monitor network fetch
  //   await Future.delayed(Duration(milliseconds: 10000));
  //   print('after Load');
  //   // if failed,use loadFailed(),if no data return,use LoadNodata()
  //   _controller.loadComplete();
  // }

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
        controller: _controller,
        enablePullUp: true,
        enablePullDown: true,
        header:
            CustomHeader(builder: (BuildContext context, RefreshStatus mode) {
          return Container(
            height: 120,
            alignment: Alignment.bottomCenter,
            child: Image.asset(
              'static/images/loading.gif',
              width: 60,
              height: 60,
            ),
            decoration: BoxDecoration(color: color ?? Config.MainColor),
          );
        }, onOffsetChange: (offset) {
          //do some ani
        }),
        // header: Text('加载'),
        onRefresh: onRefresh,
        child: _child,
        onLoading: onLoading,
        footer: CustomFooter(
          builder: (BuildContext context, LoadStatus status) {
            switch (status) {
              case LoadStatus.loading:
                {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Transform.scale(
                        scale: 0.8,
                        child: RefreshProgressIndicator(
                          strokeWidth: 3.0,
                        ),
                      ),
                      Text('数据加载中')
                    ],
                  );
                }
              case LoadStatus.failed:
                {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Transform.scale(
                        scale: 0.8,
                        child: RefreshProgressIndicator(
                          strokeWidth: 3.0,
                        ),
                      ),
                      Text('数据加载失败')
                    ],
                  );
                }
              case LoadStatus.idle:
                {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Transform.scale(
                        scale: 0.8,
                        child: RefreshProgressIndicator(
                          strokeWidth: 3.0,
                        ),
                      ),
                      Text('数据加载中')
                    ],
                  );
                }
              case LoadStatus.canLoading:
                {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Transform.scale(
                        scale: 0.8,
                        child: RefreshProgressIndicator(
                          strokeWidth: 3.0,
                        ),
                      ),
                      Text('数据加载中')
                    ],
                  );
                }
              case LoadStatus.noMore:
                {
                  return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [Text('没有更多了')],
                  );
                }
            }
          },
        ));
  }
}
